/*
Developed by Oliver Gorzellik at Wittig Technologies AG (c) 2005-2007
Review partial by Thomas M. Wittig (TMW) from June 2008
TMW: "Some bugs found, not easy to understand software structure"
TMW: "All to be improved!!!"
-------------------------------------------------------------------------------------------------------------------------
Open source development
-------------------------------------------------------------------------------------------------------------------------
Author	:	Hayo.W (blueflash)
Sign	:	BF
Date	:	05.Nov.2008
version	:	1.2.BF.0.1

-------------------------------------------------------------------------------------------------------------------------
Open source development history
-------------------------------------------------------------------------------------------------------------------------
| 05.Nov.2008 | 1.2.BF.0.1   | New FFT implementation prototype - no function yet
| 06.Nov.2008 | 1.2.BF.0.5   | Changed grid division and size (from 600 x 384 to 600 x 400)
| 09.Nov.2008 | 1.2.BF.0.6   | New grid for XY-Mode, changed offsets and calculating values due to
|             |              | the new grid dimension and divisions. Replaced numeric values with
|             |              | global defined constants for better maintainance.
|             |              | Testing two different grid layouts (standard oszi with solid lines versus
|             |              | proprietary layout with dotted lines). Optional switching via menue maybe an idea.
| 10.Nov.2008 | 1.2.BF.0.7   | Fixed some offsets and test some other rotary timings
| 10.Nov.2008 | 1.2.BF.0.8   | Fixed some offsets and test some other rotary timings
| 10.Nov.2008 | 1.2.BF.0.9   | New Grid for delayed mode with dimension 600 x 400 - takes no effect because of loading
|             |              | the grid from flash
| 11.Nov.2008 | 1.2.BF.0.10  | Fixed some values in the grids and planes, direct draw of delayed grid - no flash load
| 11.Nov.2008 | 1.2.BF.0.11  | Draw new startscreen after setup default values, reactivated UIPlane3 in function
|             |              | ClearMenuItem()
| 12.Nov.2008 | 1.2.BF.0.12  | New button handler functions for function keys 1 - 6 in main button handler for better
|	      |              | readable structure.
|             |              | Test if grids are loaded from flash - switch off grid drawing
| 12.Nov.2008 | 1.2.BF.0.13  | No flashloading for the grids anymore, all grids are drawn directly
| 13.Nov.2008 | 1.2.BF.0.14  | Changed menu structure for switching Main/Delayed/XY mode, fixed failure in delayed grid
| 13.Nov.2008 | 1.2.BF.0.15  | Added LED test in Hardware init routine
| 14.Nov.2008 | 1.2.BF.0.16  | Fixed grid overwriting when popdown is called 
| 14.Nov.2008 | 1.2.BF.0.17  | Changed LED test, change some calculations in Cursor function to fix x-cursor offset,
|             |              | corrected Voltage offset due to new grid dimension,
| 14.Nov.2008 | 1.2.BF.0.18  | Corrected trigger cursor position in delayed window, added comments for better understanding
| 14.Nov.2008 | 1.2.BF.0.19  | Added floatprintf() because printing float values to RS232 with printf causes
|             |              | a systemcrash in open source FW
| 18.Nov.2008 | 1.2.BF.0.20  | Corrected the pre trigger calculating routine and added new time base table 
| 19.Nov.2008 | 1.2.BF.0.21  | Corrected the floating point printing routine. Values are printed with after dot
|             |              | positions now. Fixed overflow and underrun and cleaned up the variable declarations
|             |              | in this routine.
| 20.Nov.2008 | 1.2.BF.0.22  | Renamed global variable Trig_Mem_Pos into MemWinStart. Corrected trigger cursor Y-position
|             |              | 2 Pixels down. Corrected names of signal drawing routines due to vector and non vector 
|             |              | Corrected menu check for vector drawing in DRAWSIGNALS()
| 21.Nov.2008 | 1.2.BF.0.23  | New default values for zero levels, deleted some unused variables
| 23.Nov.2008 | 1.2.BF.0.26  | Fallback from version 0.25 to 0.23 added manual ADC correction to reduce signal noise, which is 
|             |              | generated by poor adjustment of the offsets between the 4 ADCs of each channel
| 03.Dec.2008 | 1.2.BF.0.27  | Changed signal drawing routines, changed ADC-routines and offset calculation, new function
|             |              | for auto ADC correction to reduce signal noise, which is generated by poor adjustment of the
|             |              | offsets between the 4 ADCs of each channel
| 03.Dec.2008 | 1.2.BF.0.28  | Bug in function RenderText() fixed, changed zero correction
| 05.Dec.2008 | 1.2.BF.0.29  | changed complete signal drawing routines
| 07.Dec.2008 | 1.2.BF.0.30  | changed complete signal drawing routines
| 16.Dec.2008 | 1.2.BF.0.31  | changed complete signal drawing routines, implemented some test switches
| 17.Dec.2008 | 1.2.BF.0.32  | optimized interrupt handling, no Signal output when Interrupt occured
| 31.Jan.2009 | 1.2.BF.0.33  | some further changes in the drawing routine, especially in delayed mode and math channel
|	      |              | still buggy: offset in delayed window, spikes on timebase < 50ns produced while interpolation 
| 01.Feb.2009 | 1.2.BF.0.34  | fixed the spike problem in interpolation routine -> complete new simplified (and faster!) interpolation
| 01.Feb.2009 | 1.2.BF.0.35  | deleted old interpolation routine, freed unused resources
| 04.Feb.2009 | 1.2.BF.0.36  | new math calculating due to the new drawing routine, added new level 0 function ProcessSignalData() in 
|             |              | class Display and transferred the math routines from ADC-handler to it. Fixed Pmin - Pmax drawing.
| 07.Feb.2009 | 1.2.BF.0.37  | Changed handling of UI interrupt again. Deleted the unused function ClearFlips(). Added new 
|             |              | level 0 function Transfer_USB_Data() and moved coding from Handle_ADC() to it
| 10.Feb.2009 | 1.2.BF.0.38  | redesigned the math functions with new scaling and offset calculation, changed rotation direction of
|             |              | math menu to direction of the other settings (left -> ascending, right -> descending)
| 27.Feb.2009 | 1.2.BF.0.39  | Second prebeta version, improved math-function, new scaling, offset adjusting has still to be done... 
|             |              | math support for delayed mode added
| 28.Feb.2009 | 1.2.BF.0.40  | deactivated ADC calibration for channel 3 + 4 because of missing test environment, changed some 
|             |              | interpolation parameters and renamed function preinterpolate() into interpolate()
| 03.Mar.2009 | 1.2.BF.0.41  | implemented math offset, fixed problems in 10ns and 20ns timebase, optimized drawing routine (faster)
| 05.Mar.2009 |              | renamed some variables, deleted unused resources, cleaned up sources, corrected point drawing (non vector)
|             |              | added interpolation for math channel
| 20.Mar.2009 | 1.2.BF.0.42  | fixed inversion bug, corrected math zero calculation, corrected drawing Y-offset, reactivated 
|             |              | zero calibration for channel 3 + 4
| 22.Mar.2009 | 1.2.BF.0.43  | capsulated math functions in separate function modules, redesigned processing of drawing routines,
|             |              | optimized coding, added interpolation for 5ns
| 23.Mar.2009 | 1.2.BF.0.44  | added math calculation for delayed signal, fixed drawing offsets, cleaned up main drawing routine,
|             |              | fixed memory overflow in interpolation routine
| 24.Mar.2009 | 1.2.BF.0.45  | redesigned interface of T-Y drawing routine, deleted unused variables, first beta version!
| 26.Mar.2009 | 1.2.BF.0.46  | complete new X-Y drawing routine with new interface and vector function, line drawing is very slow
| 30.Mar.2009 | 1.2.BF.0.47  | complete new (faster) line drawing function under construction, implemented new experimental 
|             |              | interpolation timebase 5ns
| 30.Mar.2009 | 1.2.BF.0.48  | implemented Bresenham line drawing algorithm which is much faster now (about 10 times), deactivated
|             |              | new timebase for the new beta release because of overflow problems -> will be reactivated later,
|             |              | added solid / dotted lines for XY-Grid
| 31.Mar.2009 | 1.2.BF.0.49  | line() drawing is default now because of the new faster line() function which is not slower now
|             |              | than the optimized signal drawing but shows more details, correkted  direction of X-deflection, 
|             |              | storing ZeroLevel, mathvalues, alpha-correction and ADC-correction in config flash,
|             |              | implemented new ground functionality which writes real zero (ADC_ZERO) to the signal buffer instead of 
|             |              | just drawing a line at zerolevel, activated zero adjustment for XY-mode, changed the rotary characteristic
|             |              | to square function, fixed XY-zero adjustment bug
| 05.Apr.2009 | 1.2.BF.0.50  | XY-support for channels 3 + 4, XY-zerolevels are stored in flash now
| 14.Apr.2009 | 1.2.BF.0.51  | deleted some unused variables, changed default for zeroes, changed init routine, switched of zero signs for
|             |              | channel 3 + 4 in XY-Mode, changed menu handling for XY-mode
| 18.Apr.2009 | 1.2.BF.0.52  | reactivated new designed zero shift, calibrated scale factors, calibrated shift factors,
|             |              | new square function characteristic 1/4 * X² + X for zero wheels and triggerlevel, added new interpolated
|             |              | timebase 5ns -> still some problems, corrected trigger time offsets, fixed pixel failure in trigger cursor
|             |              | new square function characteristic 1/2 * X² + X for memory position wheel
| 21.Apr.2009 | 1.2.BF.0.53  | changed square function characteristic to X² + X for memory position wheel, fixed bug in ADC-calibration
|             |              | corrected  timebase selection in timebase wheel handler for new timebase 5ns, new cubic charactistic 
|             |              | X³ + X for position wheel, blocking delayed mode for timebase 5ns, new virtual zero for ground coupling,
|             |              | fixed shifted inversion bug
| 21.Apr.2009 | 1.2.BF.0.54  | new zero calibration
| 08.May.2009 | 1.2.BF.0.55  | new fast integer scaling in drawing routine, added testfunction 1 and 2 written by Guido, pixel connecting 
|             |              | is active for default again to increase speed 
| 08.May.2009 | 1.2.BF.0.56  | fixed bug in XY-drawing routine in single point mode reported by Kurt Bohnen, optimized drawing speed by
|             |              | scaling without any multipliing,complete new calibration unit, new Button for Calibration in the utility menu,
|             |              | fixed bug in SearchZero() for channel 3 + 4
| 11.May.2009 | 1.2.BF.0.57  | changed DAC-calibration, renamed variable Channelx_Zero_Sign_Offset_y into CHx_DAC_Correction_y corresponding
|             |              | to the real function of this variable,
| 12.May.2009 | 1.2.BF.0.58  | searching for freeze-bug, -> got it, 
| 15.May.2009 | 1.2.BF.0.59  | changed ADC start/stop logic to fix freeze bug, detected new timebase bug wich is also present in actual FW 1.4
|             |              | -> timebases greater than 20s are running in 50ns mode, changed config saving in new ADC-calibration routines,
|             |              | changed UI-request handling - flag is deleted in UI-handlers only now,
| 15.May.2009 | 1.2.BF.0.60  | change zero level correction variables to array, renamed VoltageCorrectionfloat to ZeroScaleFactor
|             |              | adjusted ZeroScaleFactors, stored new grid and draw settings into Flash
| 16.May.2009 | 1.2.BF.0.61  | cleaned up sources from obsolete and unused coding (rollmode etc.), DAC-calibration only for active channels,
| 16.May.2009 | 1.2.BF.0.62  | optimized DAC-calibration and adjusted scale factors for zero level, found some register settings for 
|             |              | channel 3 + 4 that must be checked -> SetupADC() -> changed register settings
| 16.May.2009 | 1.2.BF.0.63  | fixed "cut of signal" bug which was found and solved by Guido, repaired trigger problem which occured
|             |              | with version 0.60 because of deletion of some scaling factors, fixed remaining pixel error in TransferPlanes()
|             |              | which was solved by Guido, ADC-assembler routines changed by Stefan to fix correction error in averaging and
|             |              | inverting mode, fixed "undeleted trigger cursor error" which left sometime "dead" cursors on the screen after
|             |              | using default setup.
| 20.May.2009 | 1.2.BF.0.64  | implemented roll and shift mode for ultra slow timebases, new buttons in timebase menu for roll/shift mode,
|             |              | forced auto trigger for USTB-mode,
| 28.May.2009 | 1.2.BF.0.65  | added Stefans fixes for triggering, cleaned up ReadOut_Signal() from obsolete parameter found by Stefan,
|             |              | added Sefans definitions for menu structure, added similar fix like Stefans in USB-Routine,
|             |              | added correction for trigger values
| 28.May.2009 | 1.2.BF.0.66  | missing cursor data fixed by Guido
| 28.May.2009 | 1.2.BF.0.67  | changed USTB timer control from start/stop to continiuos, deactivated all 
|             |              | timer 1 routines -> useless stuff, deleted CopySignal() -> unused, fixed XY-Mode freeze in slow timebases,
|             |              | implemented USTB-support for XY-mode, fixed Menu inconsistency after start, improved "restart memory" -> DSO 
|             |              | "remembers" now about timebase menu settings, fixed interrupt initialisation at starting sequence,
| 01.Jun.2009 | 1.2.BF.0.68  | Changed menu structure for display menu,timebase menu and utility menu
| 03.Jun.2009 | 1.2.BF.0.69  | redesigned USTB timing control -> new ultra slow timebases 2S/1S/500mS, fixed minor bug memory window
|             |              | wrong initialized, 
| 03.Jun.2009 | 1.2.BF.0.70  | optimized USTB-refresh, changed begin of USTB-Range to 1S - 500mS is a normal TB now
| 07.Jun.2009 | 1.2.BF.0.71  | implemented Guidos harware C-routines as testfunction (shift + S) 
| 08.Jun.2009 | 1.2.BF.0.72  | separated C-routines in own new functions to avoid cross effects, C-routines are available via testswitch 1
|             |              | (shift + S) for channel 1 only,
| 09.Jun.2009 | 1.2.BF.0.73  | implemented new FFT routines, new FFT grid, reactivated and changed FFT menu structure, added FFT drawing routine
|             |              | added test signal generator in utility menu, added FFT spectral analysis function, changed trigger offset calculation
|             |              | testet interpolation with low pass filter, but results are worse than simplified routine -> commented coding out
| 13.Jun.2009 | 1.2.BF.0.74  | corrected zoom factor table and zoom index table for correct triggering in 5nS + 2nS TB, changed scroll sensitivity
|             |              | and scroll speed for pretrigger and memory position, in interpolated TB the single point option shows only real
|             |              | values - interpolated values are dropped (for Bruno), when switching drawing mode a popup is shown (for Bruno),
|             |              | added signal coupling support for test signal generator,
| 15.Jun.2009 | 1.2.BF.0.75  | fixed bug in test signal generator
| 15.Jun.2009 | 1.2.BF.0.76  | FFT is running with floating point calculation now, implemented FFT with 512 and 1024 points
| 20.Jun.2009 | 1.2.BF.0.77  | implemented integer calculation for FFT - performance increased by factor 8 - 10!
| 25.Jun.2009 | 1.2.BF.0.78  | implemented fast table based prescaled integer logarithm for FFT, new functions for centering and
|             |              | restoring zero levels, fixed channel number display bug and changed status bar update, changed Math and FFT menu logic,
|             |              | repared menu update logic, repaired math zero level correction,
| 28.Jun.2009 | 1.2.BF.0.79  | added new c-coding from Guido for readadc_all(), added new screenshot function from Niklas
| 28.Jun.2009 | 1.2.BF.0.80  | new FFT-scaling,
| 28.Jun.2009 | 1.2.BF.0.81  | fixed channel 3 + 4 restore settings when coming back from FFT, placed Niklas screenshot() function directly on 
|             |              | Quick Print button and deactivated QP-Menu, popuptimer is deactivated when screenshot is active,
|             |              | cleaned up UART ISR
| 03.Jul.2009 | 1.2.BF.0.82  | added complete redesigned QM function -> credits to Stefan
| 04.Jul.2009 | 1.2.BF.0.83  | redesined and improved FFT functions, redesigned and optimized zero sign drawing, redesigned pretrigger calculation, 
|             |		     | adjusted vertical cursor data, fixed single shot and start/stop problem when changing timebase, 
|             |		     | corrected Y-position of memory window, fixed dimension bug in floatstr.cpp, repaired button drawing,
|             |		     | added two step logic for delta cursor data,
| 12.Jul.2009 | 1.2.BF.0.84  | added Falks screenshot improvements, added two step logic for QP-button,
| 14.Jul.2009 | 1.2.BF.0.85  | added new noise suppression in ACQUIRE menu, added display layout switch in FFT menu
| 14.Jul.2009 | 1.2.BF.0.86  | fixed button logic in acquire menu, fixed double shot problem at QP, fixed error in FFT-windows selection,
|             |              | new direct FFT source selection via channel buttons,
| 19.Jul.2009 | 1.2.BF.0.87  | new class and file structure, changed main program from object processing to static calls
| 21.Jul.2009 | 1.2.BF.0.88  | changed refresh control of signal drawing for better zerolevel adjustment, added new screenshot version from Falk,
|             |              | optimized rotary characteristic for pretrigger adjustment, 
| 22.Jul.2009 | 1.2.BF.0.89  | fall back to screenshot version 0.4 because of problems,
| 24.Jul.2009 | 1.2.BF.0.90  | modified screenshot version 0.4 to 13 Planes (without memory planes) -> 0.4BF, fixed QM active after direct screenshot,
|             |              | new QP menu structure,
| 24.Jul.2009 | 1.2.BF.0.91  | improved noise filter, added noise filter support for interpolated timebases,
| 03.Aug.2009 | 1.2.BF.0.92  | Holiday Edition - new adc_change-register setting inspired by Falk, register settings are not loaded from protected 
|             |              | flash any more, implemented USTB-support for auto scale (ticket 3), switched of the forced AC-coupling at autoscaling
|             |              | changed signal drawing in accurate drawing mode (with line() function) to suppress screen output when the line segment
|             |              | is out of range (ticket 7), repaired and improved save/recall function, solved trigger mode problem after recalling
|             |              | signals (ticket 5), added full USTB support for save/recall function, fixed channel active LEDs error,
|             |              | found spike problem at end of signal buffers, implemented solution for the spike problem (new adc_change-register setting), 
|             |              | fixed Math-LED error (ticket 26), fixed text layer problem -> fixed delta cursor text deletion problem (ticket 8), 
| 09.Sep.2009 | 1.2.BF.0.93  | fixed text layer deletion problem while screen-refresh (no text in popup-menu), fixed undo autoscale bug,
|             |              | fixed grid intensity bug, reorganized main led control for all menus, fixed cursordata deletion bug, implemented new
|             |              | startup logic with new restoring from flash, implemented new trigger source change logic when switching on/off the channels,
|             |              | repaired first display line of background in top area (layer 3)
| 28.Sep.2009 | 1.2.BF.0.94  | updated Falks remote interface to actual version,
| 29.Sep.2009 | 1.2.BF.0.95  | updated UART-ISR to actual version, 
| 30.Sep.2009 | 1.2.BF.0.96  | fixed "shifting signal" problem in stop mode and noise filter active (same problem as in ticket 39), implemented new 
|             |              | single shot logic, changed UI-request handling for rotary interface, cleaned up sources and unused variables
| 05.Oct.2009 | 1.2.BF.0.97  | fixed auto scale trigger restore, new rotary handling in stop mode, shifted startup sequence from rotary interface
|             |              | to own function module, fixed freeze problem when turning zerolevel of inactive channel,
|             |              | corrected and optimized zero sign drawing, updated Falks remoteinterface from OS, updated QM-function from OS,
|             |              | optimized UI_request handling for zero level adjustment,
| 20.Mar.2010 | 1.2.BF.0.98  | added #001 channel delay compensation, fixed splash freeze which appeared with 0.97, new utility submenu, 
|             |              | added #002 new variable register setting in utility submenu, added new function to get
|             |              | register factory setting from protected flash, fixed some minor bugs, optimized Startup(),
|             |              | made remote interface and screenshot compatible to actual OS-version (92a), implemented Stefans new combi-trigger,
|             |              | redesigned DAC-calibration, transferred DAC-correction values from config to protected flash,
| 28.Mar.2010 | 1.2.BF.0.99  | with gain switch (shift + G) the hardware amplification for the 2er and 1er voltage ranges can be switched between
|             |              | 1 (standard) and 1.25 -> this reduces the noise in the 1er and 2er ranges, with the third gain index there
|             |              | is a compensated scale factor available for devices with modified resistances in the preamp output, added new
|             |              | test value in ADC-Setting
| 16.Apr.2010 | 1.2.BF.0.100 | #003 redesigned delayed mode -> ticket #34
| 20.Apr.2010 | 1.2.BF.0.101 | #004 new utility menu structure with settings for preamp gain 
| 20.Apr.2010 | 1.2.BF.0.102 | fixed Auto trigger bug in USTB-Mode and Single-Mode which occured because of the new Combi trigger, fixed Continiuos Bug
|             |              | in "About Oscilloscope" menu
| 21.Apr.2010 | 1.2.BF.1.0   | first stable release! Cleaned up coding from unused functions and variables, added new menu init function #005,
|             |              | freed unused memory area, checked ticket #48 -> seems ok, Ticket #50 fixed, checked Ticket #24 -> seems ok,
|             |              | Ticket #37 fixed, fixed double writing to flash at timebase change, checked ticket #35 -> seems to be ok,
|             |              | added button in Quick Print menu to choose screenshot version #006, added menu popup init function #007
| 23.Apr.2010 | 1.2.BF.1.1   | recalculated and adjusted all scaling factors, new scaling for 33 Ohm setting and gain 1.25 #008, fixed exchanged entries in 
|             |              | ADC-Setup menu, made screenshot function work more stable,
| 27.Apr.2010 | 1.2.BF.1.2   | solved ticket #31 -> implemented QM support for interpolated Tb #009, corrected menu entries in QM selection,
|             |              | added QM support for ultra slow timebases, 
| 01.May.2010 | 1.2.BF.1.3   | fixed channel bug at switching FFT, reorganized flash routines and fixed several problems #010, stabilized startup routine,
|             |              | added new flash backup areas in unused sectors, fixed switching logic in math menu, added special flash load logic,
|             |              | new FFT-grid frequency division which is switchable via multiple pushing grid switch in the display menu, 
|             |              | stabilized channel switch logic,fixed some bugs in the FFT-prepare sequence,
| 08.May.2010 | 1.2.BF.1.4   | added Save/Recall support for FFT, new restore button in Save/Recall menu, fixed USTB timer 2 bug which was caused by
|             |              | the new flash routines, made grid switching logic more stable, improved the Run/Stop function for FFT and for switching
|             |              | between FFT and time mode,
| 10.May.2010 | 1.2.BF.1.5   | changed flash writing logic to make it faster but also save, implemented new overlay function but let the menu inactive #013,
|             |              | new RMS calculation for quick measure #014, 
| 26.May.2010 | 1.2.BF.1.6   | reorganized Save/Recall #016, new USB routines under construction #017, repaired WELEC USB interface, communication with
|             |              | PC-software works partly - some bugs are in the PC-sofware and can't be fixed, WELEC USB-Firmware update works but needs
|             |              | 1 hour and twenty minutes -> not usable, fixed signal freeze at lower timebases caused by ISR of timer 3 (browswer window) #018,
|             |              | zero signs are displayed in USTB-Mode now,
| 10.Jun.2010 | 1.2.BF.1.7   | deactivated the browse function for USTB mode, deleted pretrigger sign in USTB mode, corrected and reorganized external
|             |              | trigger settings and level #019, deactivated Reject Button in Trigger Mode/Coupling menu #020 because of no function,
|             |              | repaired source channel setting for puls width triggering #021,
| 10.Jun.2010 | 1.2.BF.1.8   | set new values for external trigger default setup, changed register settings for ext. LF Reject / HF Reject / Line (-> Jürgen)
| 10.Jun.2010 | 1.2.BF.1.9   | tried to correct the channel 3+4 problem at external triggering (signal only every second time) #022 but found no solution
| 10.Aug.2010 | 1.2.BF.1.10HE| activated and completed the overlay function for trace recall, repaired Save/Recall bug depending on wrong sector offset 
|             |              | which produced data trash on channel 3 + 4, fixed menu bug in trigger mode popup when switching between normal and USTB-mode,
|             |              | improved menu logic for USTB <-> normal switching,
--------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
